package com.demo;

import dao.jdbc;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import pojo.MsgInfo;

import javax.servlet.http.HttpServletRequest;

@Controller
@RequestMapping("/")
public class index {
    jdbc jd = new jdbc();

//    @RequestMapping(value = "", method = RequestMethod.GET)
//    public String index() {
//        return "index";
//    }

    /**
     * 注册接口
     *
     * @param request
     * @param account
     * @param cellphone
     * @param mailbox
     * @param password
     * @return
     */
    @RequestMapping(value = "register", method = RequestMethod.POST)
    public ResponseEntity<MsgInfo> register(HttpServletRequest request, String account, String cellphone, String mailbox, String password) {
        try {
            MsgInfo msg = new MsgInfo();
            msg.setStatus(0);
            msg.setMsg("注册失败或邮箱已存在！");
            if (jd.registers(account, password)) {
                msg.setStatus(1);
                msg.setMsg("注册成功");
            }
            return ResponseEntity.ok(msg);
        } catch (Exception e) {
            return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(null);
        }
    }

    ;

    /**
     * 登录接口
     *
     * @param request
     * @param account  账号
     * @param password 密码
     * @return
     */
    @RequestMapping(value = "login", method = RequestMethod.POST)
    public ResponseEntity<MsgInfo> login(HttpServletRequest request, String account, String password) {
        try {
            MsgInfo msg = new MsgInfo();
            msg.setStatus(0);
            msg.setMsg("登录失败");
            if (jd.login(account, password)) {
                msg.setStatus(1);
                msg.setMsg("登录成功");
            }
            return ResponseEntity.ok(msg);
        } catch (Exception e) {
            return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(null);
        }
    }
}
